package org.abratuhi.kvngroupware.brainstorm.widget;

import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;

import org.abratuhi.kvngroupware.KVNGroupwareClientAction;
import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.model.Brainstorm;
import org.abratuhi.kvngroupware.login.manager.LoginManager;
import org.abratuhi.kvngroupware.manager.Manager;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.spring.SpringUtilities;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.user.model.User;
import org.abratuhi.kvngroupware.widget.AbstractUIWidget;
import org.jdom.Element;

@SuppressWarnings("serial")
public class BrainstormlistWidget extends AbstractUIWidget{
	LoginManager loginmanager;
	
	JButton btnCreate = new JButton();
	JButton btnJoin = new JButton();
	JButton btnDelete = new JButton();

	public BrainstormlistWidget(String frametitle, MessageSender sender,
			Properties locale, LoginManager loginmanager, Manager<Brainstorm> brainstormmanager) {
		super(frametitle, sender, locale, brainstormmanager);
		this.loginmanager = loginmanager;
		
		this.btnCreate.setAction(new CreateAction(locale.getProperty("uiwidget.button.create"), null, null, null));
		this.btnJoin.setAction(new JoinAction(locale.getProperty("uiwidget.button.join"), null, null, null));
		this.btnDelete.setAction(new DeleteAction(locale.getProperty("uiwidget.button.delete"), null, null, null));
		
		this.buttonpanel.setLayout(new SpringLayout());
		this.buttonpanel.add(btnCreate);
		this.buttonpanel.add(btnJoin);
		this.buttonpanel.add(btnDelete);
		SpringUtilities.makeCompactGrid(buttonpanel, 1, 3, 0, 0, 0, 0);
		
		revalidate();
	}

	@Override
	public void onFrameClose() {}
	
	class CreateAction extends KVNGroupwareClientAction{
		public CreateAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			String newBrainstormTheme = JOptionPane.showInputDialog("Enter Brainstorm Theme:");
			
			User user = loginmanager.getUser();
			Team team = loginmanager.getTeam();
			Brainstorm brainstorm = new Brainstorm(user.getId()+new Date().getTime(), team.getId(), newBrainstormTheme);

			Message message = sender.createMessage(
					"type,action,brainstormId,userId,teamId,brainstormTheme", 
					"brainstorm,create,"+brainstorm.getId()+","+user.getId()+","+team.getId()+","+brainstorm.getTheme(),
					new String(),
					new Element[0]);
			sender.sendMessage(message);
		}
	}
	
	class JoinAction extends KVNGroupwareClientAction{
		public JoinAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(datalist.getModel().getSize()>0 && datalist.getSelectedIndex()!=-1){
				String selected = (String) datalist.getSelectedValue();
				String[] split = selected.split("|");
				String selectedBrainstormId = split[1];

				User user = loginmanager.getUser();
				Team team = loginmanager.getTeam();
				Brainstorm brainstorm = ((BrainstormManager)manager).get(selectedBrainstormId);

				Message message = sender.createMessage(
						"type,action,brainstormId,userId,teamId,brainstormTheme", 
						"brainstorm,join,"+brainstorm.getId()+","+user.getId()+","+team.getId()+","+brainstorm.getTheme(),
						new String(),
						new Element[0]);
				sender.sendMessage(message);
			}
		}
	}
	
	class DeleteAction extends KVNGroupwareClientAction{
		public DeleteAction(String text, ImageIcon icon, String desc, Integer mnemonic) {
			super(text, icon, desc, mnemonic);
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			if(datalist.getModel().getSize()>0 && datalist.getSelectedIndex()!=-1){
				String selected = (String) datalist.getSelectedValue();
				String[] split = selected.split("|");
				String selectedBrainstormId = split[1];

				User user = loginmanager.getUser();
				Team team = loginmanager.getTeam();
				Brainstorm brainstorm = ((BrainstormManager)manager).get(selectedBrainstormId);

				Message message = sender.createMessage(
						"type,action,brainstormId,userId,teamId,brainstormTheme", 
						"brainstorm,delete,"+brainstorm.getId()+","+user.getId()+","+team.getId()+","+brainstorm.getTheme(),
						new String(),
						new Element[0]);
				sender.sendMessage(message);
			}
		}
	}

}
